#!/bin/bash
#setupdate:2020-11-25
#updatedate:2021-3-11
#author:Jinglin Han
#####################################################
#Usage
usage(){
echo """
Usage	:
	anchor_binning [option] -r <reference> -q <query> -p <prefix>

Options	:
	-r FILE		reference in fasta format ;
	-q FILE		query in fasta format ;
	-p String	prefix ;
	--clear		remove temporary files[save temporary files] ;
	-h/--help       this message ;
"""
}
#####################################################
##--------------------params-----------------------##
ref=
qry=
wd=`dirname $0`
src=${wd}/src/
prefix=
clean=false
##--------------------envs-------------------------##
MINIMAP=`which minimap2`
JAVA=`which java`
PYTHON=`which python`

if [[ $# == 0 ]] ;then
	usage
	exit 0;
fi
echo "CMD :$0 $*"
while [[ $# > 0 ]]
do
	case $1 in
	"-h")
	usage
	exit 0
	;;
	"--help")
	usage
	exit 0
	;;
	"-r")
	ref=$2
	shift
	;;
	"-q")
	qry=$2
	shift
	;;
	"-p")
	prefix=$2
	shift
	;;
	"--clear")
	clean=true
	shift
	;;
	*)
	echo "invalid parameters : \"$1\""
	exit 1
	;;
	esac
	shift
done
########################################################
##----------------------step1-------------------------##
#mapping with minimap2#
${MINIMAP} -cx asm5 -t8 --cs ${ref} ${qry} >${prefix}.asm.paf
##----------------------step2-------------------------##
#filter#
awk '{if($12>=60)print}' ${prefix}.asm.paf | awk '{if(($9-$8)>1000)print}' - >${prefix}.asm_filter.paf
##----------------------step3-------------------------##
#greedy#
sort -k1 ${prefix}.asm_filter.paf >${prefix}.asm_filter_sorted.paf
${JAVA} -jar ${src}/greedy.jar ${prefix}.asm_filter_sorted.paf 0.1 >${prefix}.asm_filter_no_overlap.paf
##----------------------gaptake-----------------------##
#gap take#
awk '{print $1,$3,$4,$6,$8,$9,$5}' ${prefix}.asm_filter_no_overlap.paf | sort -k5,5n >temp.txt
${JAVA} -jar ${src}/getgap.jar temp.txt >temp_1.txt
awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t""gap"}' temp_1.txt >${prefix}.gap.txt
##----------------------step4-------------------------##
#anchor#
${JAVA} -jar ${src}/extract_cigar_info.jar ${prefix}.asm_filter_no_overlap.paf >${prefix}.match_point
##----------------------step5-------------------------##
#binning#
${JAVA} -jar ${src}/binning.jar ${prefix}.match_point 10000 >temp_2.txt
awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t""anchor"}' temp_2.txt>${prefix}.anchor.txt
##----------------------mergefiles--------------------##
#merge gap bin file and anchor bin file#
cat ${prefix}.anchor.txt ${prefix}.gap.txt >${prefix}.bin
##----------------------CalcEditDist------------------##
${PYTHON} ${src}/run.py -ref ${ref} -query ${qry} -anchor ${prefix}.bin -o ${prefix}.out
sort -k2,2n ${prefix}.out/${prefix}.bin-result >${prefix}.final.bin
##----------------------Draw--------------------------##

########################################################
##----------------------removetempfile----------------##
clean(){
rm temp.txt
rm temp_1.txt
rm temp_2.txt
rm ${prefix}.anchor.txt
rm ${prefix}.gap.txt
}
if $clean ;then clean
fi
